package com.brayan.webapp.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

@Entity
public class Pub implements Serializable {

	private static final long serialVersionUID = -7271922017926181308L;

	
	public Pub() {
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	// Meta
	@Column(columnDefinition = "boolean default true")
	private boolean active = true;
	/** The higher, the more important to display */
	@Column(columnDefinition = "int default 1")
	private int severityLevel = 1;
	private Calendar pubDate;
	private String mediaSource;
	private String location;
	/** One must use cascade, otherwise referenced object cannot be saved */
	@ManyToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
	private Set<UploadedImage> uploadedImages;
	private Calendar dateCreated;
	private Calendar dateModified;

	// Content
	@Column(nullable = false, length=512)
	private String title;
	@Column(length=1024)
	private String teaser;
	@Column
	@Lob
	private String article;

	// References
	/** Set to eager, so that associated tuples are fetched, too. Set default 1 (Private) */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "pubCategoryId")
	private PubCategory pubCategory;

	@ManyToMany(fetch = FetchType.EAGER)
	private List<PubThread> pubThreads;

	public List<PubThread> getPubThreads() {
		return pubThreads;
	}

	public void setPubThreads(List<PubThread> pubThreads) {
		this.pubThreads = pubThreads;
	}

	/***** EVENT HOOKS *****/
	@PrePersist
	protected void onCreate() {
		dateCreated = Calendar.getInstance();
	}

	@PreUpdate
	protected void onUpdate() {
		dateModified = Calendar.getInstance();
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public int getSeverityLevel() {
		return severityLevel;
	}

	public void setSeverityLevel(int severityLevel) {
		this.severityLevel = severityLevel;
	}

	/***** DATA ****/
	public Calendar getpubDate() {
		return pubDate;
	}

	public Pub setPubDate(Calendar pubDate) {
		this.pubDate = pubDate;
		return this;
	}

	public String getMediaSource() {
		return mediaSource;
	}

	public Pub setMediaSource(String mediaSource) {
		this.mediaSource = mediaSource;
		return this;
	}

	public String getLocation() {
		return location;
	}

	public Pub setLocation(String location) {
		this.location = location;
		return this;
	}

	public Set<UploadedImage> getUploadedImages() {
		return uploadedImages;
	}

	public void setUploadedImages(Set<UploadedImage> uploadedImages) {
		this.uploadedImages = uploadedImages;
	}
	
	public UploadedImage getHeaderImage(){
		//return this.uploadedImages.get(0);
		return this.uploadedImages.iterator().next();
	}
	
	public void setHeaderImage(UploadedImage headerImage){
		//this.uploadedImages.add(0, headerImage);
		this.uploadedImages.add(headerImage);
	}
	

	public String getTitle() {
		return title;
	}

	public Pub setTitle(String title) {
		this.title = title;
		return this;
	}

	public String getTeaser() {
		return teaser;
	}

	public Pub setTeaser(String teaser) {
		this.teaser = teaser;
		return this;
	}

	public String getArticle() {
		return article;
	}

	public Pub setArticle(String article) {
		this.article = article;
		return this;
	}

	public Long getId() {
		return id;
	}

	public PubCategory getPubCategory() {
		return pubCategory;
	}

	public Pub setPubCategory(PubCategory pubCategory) {
		this.pubCategory = pubCategory;
		return this;
	}

	public Calendar getDateCreated() {
		return dateCreated;
	}

	public Calendar getDateModified() {
		return dateModified;
	}

}
